home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C013.ZIP / HD.C < prev    next >
Text File  |  1990-01-19  |  2KB  |  105 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.013        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: hd.c
  7.  * Program name:hd 
  8.  * Source of file: Ron Wellstead
  9.  * Purpose: An MS-DOS copy of the UNIX utility of the same name.
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /*
  14.  *
  15.  *    @(#) hd.c 1.2 87/07/27 
  16.  *
  17.  *    UNIX style hd utility for dos
  18.  *
  19.  *    copyright (c) 1987 Ron Wellsted.
  20.  *    This software is provided on the understanding that it is
  21.  *    NOT to be used for commercial gain. It may be freely distributed
  22.  *    in source or object form among amateur and hobby computer users ONLY!
  23.  *
  24.  *    sends a hexadecimal dump of files (or stdin) to stdout
  25.  *    usage:    hd [files....]
  26.  *    written for Microsoft C, link with setargv.obj to expand wildcards
  27.  */
  28. #include    <fcntl.h>
  29. #include    <sys/types.h>
  30. #include    <sys/stat.h>
  31. #include    <io.h>
  32. #include    <stdio.h>
  33. #include    <ctype.h>
  34.  
  35. char buff[17];
  36. char what[]="@(#) hd VR 1.0.0 15 Jul 1987";
  37.  
  38. #define    FALSE    0
  39. #define    TRUE    1
  40.  
  41. long addr;
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char **argv;
  46. {
  47.  
  48.     int fh, prn_name=FALSE;
  49.  
  50.     if (argc == 1) /* no args; copy standard input */
  51.         filecopy(fileno(stdin));
  52.     else
  53.     {
  54.         if (argc>2) prn_name=TRUE;
  55.         while (--argc > 0)
  56.         {
  57.             if ((fh = open(*++argv, O_BINARY | O_RDONLY)) < 0)
  58.                 fprintf(stderr,"hd: Can't open %s\n",*argv);
  59.             else
  60.             {
  61.                 if (prn_name)
  62.                     printf("\nhd %s",*argv);
  63.                 filecopy(fh);
  64.                 close(fh);
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. filecopy(fh)    /* copy file fh to standard output */
  71. int fh;
  72. {
  73.     char ch,outline[80];
  74.     int col,i,j,n;
  75.  
  76.     addr=0L;
  77.     lseek(fh,0L,0);
  78.     do
  79.     {
  80.         j = read(fh,buff,16);
  81.         while (j<16)
  82.         {
  83.             n = 16-j;
  84.             n = read(fh,&buff[j],n);
  85.             if (n==0) break;
  86.             j += n;
  87.         }
  88.         col = sprintf(outline,"%04lx: ",addr);
  89.         for (i=0; i<j; ++i)
  90.         {
  91.             col += sprintf(&outline[col],"%02x ",0xff & buff[i]);
  92.             ++addr;
  93.         }
  94.         while (col<55) col += sprintf(&outline[col]," ");
  95.         for (i=0; i<j; i++)
  96.             if (!isprint(buff[i])) buff[i] = '.';
  97.         buff[i]='\0';
  98.         strcat(outline,buff);
  99.         printf("%s\n",outline);
  100.     }
  101.     while (j==16);
  102.     if (j) printf("%04lx: ",addr);
  103.     printf("\n");
  104. }
  105.